package com.newcoin.admin.controller;

import com.newcoin.admin.entity.Result;
import com.newcoin.admin.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

@Controller
@Slf4j
@RequestMapping(value = "/admin")
public class AdminController {


    @Autowired
    private AdminService service;

    @GetMapping(value = "/loginHtml")
    public String loginHtml() {
        return "login";
    }

    @PostMapping(value = "/login")
    @ResponseBody
    public Result login(String username, String password, HttpSession session) {
        try {
            return service.login(username, password, session);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("登录失败");
            return result;
        }
    }

    @GetMapping(value = "/")
    public String indexHtml(Model model) {
        try {
            return service.index(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @GetMapping(value = "/getUsers")
    public String getUsersHtml(Model model, Long id, String mobile, Integer add, Integer type, String start, String end,
                               Integer current) {
        try {
            return service.getUsers(model, id, mobile, add, type, start, end, current);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/forbidOrLiftForbidWithdraw")
    @ResponseBody
    public Result forbidOrLiftForbidWithdraw(Long id, Integer state) {
        try {
            return service.forbidOrLiftForbidWithdraw(id, state);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("操作失败");
            return result;
        }
    }

    @PostMapping(value = "/forbidOrLiftForbidLogin")
    @ResponseBody
    public Result forbidOrLiftForbidLogin(Long id, Integer state) {
        try {
            return service.forbidOrLiftForbidLogin(id, state);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("操作失败");
            return result;
        }
    }

    @PostMapping(value = "/editUser")
    @ResponseBody
    public Result editUser(Long id, Long pid, String mobile, String loginPwd, String payPwd, String path) {
        try {
            return service.editUser(id, pid, mobile, loginPwd, payPwd, path);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改失败");
            return result;
        }
    }

    @GetMapping(value = "/outLogin")
    public String outLogin(HttpSession session) {
        try {
            return service.outLogin(session);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @GetMapping(value = "/recharge")
    public String depositManagement(Model model, Integer current, String id, String mobile, String start, String end, Integer type) {
        try {
            return service.getRecharge(model, current, id, mobile, start, end, type);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/confirmRecharge")
    @ResponseBody
    public Result confirmRecharge(String id, Integer status) {
        try {
            return service.confirmRecharge(id, status);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("确认失败");
            return result;
        }
    }

    @GetMapping(value = "/withdraw")
    public String withdrawalManagement(Model model, Integer current, String id, String mobile, String start, String end, Integer type) {
        try {
            return service.getWithdrawal(model, current, id, mobile, start, end, type);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/dealWith")
    @ResponseBody
    public Result dealWith(String id) {
        try {
            return service.dealWith(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("处理失败");
            return result;
        }
    }

    @PostMapping(value = "/turnDown")
    @ResponseBody
    public Result turnDown(String id, String content) {
        try {
            return service.turnDown(id, content);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("驳回失败");
            return result;
        }
    }

    @GetMapping(value = "/goods")
    public String getGoods(Model model, Integer current, Integer priceStart, Integer priceEnd, String start, String end, Integer type) {
        try {
            return service.getGoods(model, current, priceStart, priceEnd, start, end, type);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/editGood")
    @ResponseBody
    public Result editGood(Long id, String goodsName, BigDecimal goodsPrice, String goodsPic, BigDecimal proportion) {
        try {
            return service.editGood(id, goodsName, goodsPrice, goodsPic, proportion);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("编辑失败");
            return result;
        }
    }

    @PostMapping(value = "/addGood")
    @ResponseBody
    public Result addGood(String goodsName, BigDecimal goodsPrice, String goodsPic, BigDecimal proportion) {
        try {
            return service.addGood(goodsName, goodsPrice, goodsPic, proportion);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("添加失败");
            return result;
        }
    }

    @GetMapping(value = "/vipSetting")
    public String getVip(Model model, Integer level, String name) {
        try {
            return service.getVip(model, level, name);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/editVip")
    @ResponseBody
    public Result editVip(Integer id, String name, Integer level, BigDecimal amount, BigDecimal fee, BigDecimal max, Integer orderNum) {
        try {
            return service.editVip(id, name, level, amount, fee, max, orderNum);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("编辑失败");
            return result;
        }
    }


    @PostMapping(value = "/addVip")
    @ResponseBody
    public Result addVip(String name, Integer level, BigDecimal amount, BigDecimal fee, BigDecimal max, Integer orderNum) {
        try {
            return service.addVip(name, level, amount, fee, max, orderNum);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(200);
            result.setMessage("添加VIP失败");
            return result;
        }
    }

    @PostMapping(value = "/deleteVip")
    @ResponseBody
    public Result deleteVip(Integer id) {
        try {
            return service.deleteVip(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("删除VIP失败");
            return result;
        }
    }

    @GetMapping(value = "/taskArea")
    public String taskArea(Model model) {
        try {
            return service.taskArea(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/editTaskArea")
    @ResponseBody
    public Result editTaskArea(Integer id, BigDecimal proportion, Integer levelCondition) {
        try {
            return service.editTaskArea(id, proportion, levelCondition);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改失败");
            return result;
        }
    }

    @GetMapping(value = "/bonus")
    public String bonus(Model model) {
        try {
            return service.bonus(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/editBonus")
    @ResponseBody
    public Result editBonus(Integer id, String name, BigDecimal proportion) {
        try {
            return service.editBonus(id, name, proportion);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改失败");
            return result;
        }
    }

    @GetMapping(value = "/product")
    public String product(Model model) {
        try {
            return service.product(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/editProduct")
    @ResponseBody
    public Result editProduct(Integer id, BigDecimal startAmount, BigDecimal endAmount, BigDecimal dailyReleaseRatio, Integer level) {
        try {
            return service.editProduct(id, startAmount, endAmount, dailyReleaseRatio, level);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改失败");
            return result;
        }
    }

    @GetMapping(value = "/ann")
    public String ann(Model model, Integer current, String title, String start, String end, Integer type) {
        try {
            return service.ann(model, current, title, start, end, type);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/addAnn")
    @ResponseBody
    public Result addAnn(String title, String content) {
        try {
            return service.addAnn(title, content);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("添加失败");
            return result;
        }
    }


    @PostMapping(value = "/offShelf")
    @ResponseBody
    public Result offShelf(String id) {
        try {
            return service.offShelf(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(200);
            result.setMessage("下架失败");
            return result;
        }
    }

    @PostMapping(value = "/editAnn")
    @ResponseBody
    public Result editAnn(String id, String title, String content) {
        try {
            return service.editAnn(id, title, content);
        } catch (Exception e) {
            Result result = new Result();
            result.setMessage("修改失败");
            result.setCode(500);
            return result;
        }
    }

    @GetMapping(value = "/carousel")
    public String carousel(Model model) {
        try {
            return service.carousel(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/addCarousel")
    @ResponseBody
    public Result addCarousel(MultipartFile file) {
        try {
            return service.addCarousel(file);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setMessage("新增失败");
            result.setCode(500);
            return result;
        }
    }

    @PostMapping(value = "/deleteCarousel")
    @ResponseBody
    public Result deleteCarousel(Integer id) {
        try {
            return service.deleteCarousel(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setMessage("删除失败");
            result.setCode(500);
            return result;
        }
    }

    @GetMapping(value = "/pay")
    public String pay(Model model) {
        try {
            return service.pay(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/editPay")
    @ResponseBody
    public Result editPay(Integer id, Integer type, String bankNum, String name, String info, MultipartFile file) {
        try {
            return service.editPay(id, type, bankNum, name, info, file);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setMessage("修改失败");
            result.setCode(500);
            return result;
        }
    }

    @GetMapping(value = "time")
    public String time(Model model) {
        try {
            return service.time(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(Integer id, String start, String end) {
        try {
            return service.save(id, start, end);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("保存失败");
            return result;
        }
    }

    @GetMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response, Integer current, String id, String mobile, String start, String end, Integer type) {
        try {
            service.exportExcel(response, current, id, mobile, start, end, type);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @GetMapping(value = "/app")
    public String app(Model model) {
        try {
            return service.app(model);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "500";
        }
    }

    @PostMapping(value = "/editApp")
    @ResponseBody
    public Result editApp(String version, String appUrl, String wGetUrl, Integer status, String remark) {
        try {
            return service.editApp(version, appUrl, wGetUrl, status, remark);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("提交失败");
            return result;
        }
    }
}
